<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load=""
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel">
        <template slot="menuLeft">
      <!--    <el-button
            v-if="permissions.pfoc_realitylinedeviceconfig_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">导出
          </el-button>-->
        </template>
        <!--产线自定义搜索-->
        <template slot-scope="scope" slot="lineIdSearch">
          <div class="block">
            <el-select  v-model="lineId" filterable placeholder="请选择实际产线">
              <el-option
                @click.native="chooseLine(item)"
                v-for="item in realityLineList"
                :label="item.realityLineName"
                :value="item.id"
                :key="item.id">
                <span style="float: left">产线【{{ item.realityLineName }}】</span>
                <span style="float: right; color: #5e80a6; font-size: 13px">品号【{{ item.articleNumber }}】</span>
                <span style="float: left; color: #5e80a6; font-size: 13px; margin: 0  5px">组织【{{ item.organizationName }}】</span>
              </el-option>
            </el-select>
          </div>
        </template>
        <!--自定义操作列-->
        <template slot-scope="scope" slot="menu">
          <el-button type="text"  icon="el-icon-setting"  size="small" plain  @click="configureOpen(scope.row)">配置</el-button>
        </template>
      </avue-crud>

      <!--配置弹窗页面-->
      <el-dialog title=""
                 :visible.sync="configureDialogVisible"
                 width="60%"
                 lock-scroll
                 :close-on-click-modal=false
                 v-if="configureDialogVisible"
                 :fullscreen="configureDialogFull">

        <template slot="title">
          <div class="avue-crud__dialog__header">
              <span class="el-dialog__title">
                  <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                  配置回显数据
              </span>
          </div>
        </template>

        <div class="pull-auto"  style="text-align: left;margin-left: 40px;font-size: 16px;color:#3478f5">
          <div class="block">
            实际产线名称：{{realityLineName}} <i class="el-breadcrumb__separator el-icon-arrow-right"></i>
            品号：{{articleNumber}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>
            组织：{{organizationName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>
            设备名称：{{deviceName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>
            设备编码：{{deviceSn}}
          </div>
          <div class="block">
          </div>
        </div>
        <!--配置页面-->
        <configurePage ref="configurePageRef"  @closeConfigurePage="closeConfigurePage" :id="id" :dsIds="dsIds" :tableNames="tableNames" :columnNames="columnNames" :orderFields="orderFields"/>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, addObj, putObj, delObj } from '@/api/pfoc/realitylinedeviceconfig'
import { getRealityLineDeviceConfigList,getRealityLineList } from '@/api/pfoc/realityproductline'
import { tableOption } from '@/const/crud/pfoc/realitylinedeviceconfig'
import { mapGetters } from 'vuex'
/**引入配置页面*/
import configurePage from '@/views/pfoc/realitylinedeviceconfig/configure'

export default {
  name: 'realitylinedeviceconfig',
  components: {
    //配置页面
    configurePage,
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      realityLineList:[],//实际产线集合
      lineId:null,//实际产线ID
      configureDialogVisible:false,//配置页面开关
      configureDialogFull:false,//配置页面弹窗缩放开关（默认放大）
      articleNumber:null,//品号
      organizationName:null,//组织名称
      realityLineName:null,//实际产线名称
      deviceSn:null,//设备编码
      deviceName:null,//设备名称
      id:null,//产线设备配置ID
      dsIds:null,//数据源ID
      tableNames:null,//表名称
      columnNames:null,//列字段
      orderFields:null,//排序字段

    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.pfoc_realitylinedeviceconfig_add, false),
        delBtn: this.vaildData(this.permissions.pfoc_realitylinedeviceconfig_del, false),
        editBtn: this.vaildData(this.permissions.pfoc_realitylinedeviceconfig_edit, false)
      }
    }
  },
  created() {
      //获取实际产线数据集合
      this.getRealityLineListData();
  },
  methods: {
    // 列表查询
    getList(page, params) {
        if(!this.validatenull(this.lineId)) {
            //通过选择实际产线获取产线设备数据
            getRealityLineDeviceConfigList(this.lineId).then(response => {
                this.tableData = response.data.data
                this.tableLoading = false
            }).catch(() => {
                this.tableLoading = false
            })
        }else{
            this.$message.warning("请选择产线。")
        }
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function(row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function(row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/pfoc/realitylinedeviceconfig/export', this.searchForm, 'realitylinedeviceconfig.xlsx')
    },
    //获取实际产线集合
    getRealityLineListData(){
        getRealityLineList().then(response => {
            this.realityLineList=response.data.data;
        })
    },
    //产线选择
    chooseLine(row){
        this.lineId = row.id;//产线ID
        this.articleNumber=row.articleNumber;//品号
        this.realityLineName=row.realityLineName;//实际产线名称
        this.getList();
    },
     //打开配置页面
    configureOpen(row){
        this.id = row.id;//产线设备配置ID
        this.deviceSn=row.deviceSn;//设备编码
        this.deviceName=row.deviceName;//设备名称
        this.organizationName=row.organizationName;//组织名称
        this.dsIds=row.dataStore;//数据源ID
        this.orderFields=row.orderField;//排序字段
        this.tableNames=row.tableName;//表名称
        this.columnNames=row.columnName;//列字段
        //打开配置页面
        this.configureDialogVisible = true;
        this.$nextTick(() => {
            //调用配置页面回显方法
            this.$refs.configurePageRef.configureInit();
        })

    },
    //关闭配置页面
    closeConfigurePage(){
        this.refreshChange();
        this.id = null;//产线设备配置ID
        this.deviceSn=null;//设备编码
        this.deviceName=null;//设备名称
        this.organizationName=null;//组织名称
        this.dsIds=null;//数据源ID
        this.orderFields=null;//排序字段
        this.tableNames=null;//表名称
        this.columnNames=null;//列字段
        this.configureDialogVisible = false;//关闭配置页面
    }
  }
}
</script>
